package com.example.data_receiver.service;


import com.example.data_receiver.entity.YfzkRecord;
import com.example.data_receiver.entity.YfzlRecord;
import com.example.data_receiver.entity.YszkRecord;
import com.example.data_receiver.entity.YszlRecord;
import com.example.data_receiver.repository.DorisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FinancialService {

    @Autowired
    private DorisRepository dorisRepository;

    private static final int BATCH_SIZE = 1000; // 每批1000条记录

    @Transactional
    public void saveFinancialRecordsYszk(List<YszkRecord> records) {
        for (int i = 0; i < records.size(); i += BATCH_SIZE) {
            List<YszkRecord> batch = records.subList(i, Math.min(i + BATCH_SIZE, records.size()));
            dorisRepository.batchInsertYszk(records);
        }
    }

    @Transactional
    public void saveFinancialRecordsYfzk(List<YfzkRecord> records) {
        for (int i = 0; i < records.size(); i += BATCH_SIZE) {
            List<YfzkRecord> batch = records.subList(i, Math.min(i + BATCH_SIZE, records.size()));
            dorisRepository.batchInsertYfzk(records);
        }
    }

    @Transactional
    public void saveFinancialRecordsYfzl(List<YfzlRecord> records) {
        for (int i = 0; i < records.size(); i += BATCH_SIZE) {
            List<YfzlRecord> batch = records.subList(i, Math.min(i + BATCH_SIZE, records.size()));
            dorisRepository.batchInsertYfzl(records);
        }
    }

    @Transactional
    public void saveFinancialRecordsYszl(List<YszlRecord> records) {
        for (int i = 0; i < records.size(); i += BATCH_SIZE) {
            List<YszlRecord> batch = records.subList(i, Math.min(i + BATCH_SIZE, records.size()));
            dorisRepository.batchInsertYszl(records);
        }
    }

}